package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.util.Arrays;

//@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String source) {

        // source : "1"     ItemType.APARTMENT
        // source : "2"     ItemType.ROOM

        // ItemType.values()  获取ItemType这个枚举中的所有选项值
        return Arrays.stream(ItemType.values()).filter(itemType -> itemType.getCode()==Integer.parseInt(source))
                .findFirst()
                .orElseThrow(IllegalArgumentException::new);
    }
}
